package com.apexedu.portal.action;

import com.apexedu.biz.entity.TAccessAppReg;
import com.apexedu.framework.service.BaseService;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.identity.entity.TSysMenu;
import com.apexedu.portal.entity.TLbxx;
import com.apexedu.portal.service.impl.PortalServiceServiceImpl;
import com.apexedu.sys.action.AbstractBaseActionSupport;
import com.apexedu.sys.entity.ApplicationEnum;
import com.apexedu.util.StringUtil;
import com.apexedu.util.json.JsonUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 批量添加服务
 * Created by janabo on 2014-10-27.
 */

@SuppressWarnings("serial")
@Component
@Scope("prototype")
public class PortalServiceAction extends AbstractBaseActionSupport {

    private List<TAccessAppReg> menuList;

    private String msg;//记录保存是否成功

    private int appno;//成功后刷新页面

    /**
     * 此方法植入service类
     */
    @Resource(name = "portalServiceService")
    public void setBaseService(BaseService baseService) {
        super.setBaseService(baseService);
    }

    public List<TAccessAppReg> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<TAccessAppReg> menuList) {
        this.menuList = menuList;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getAppno() {
        return appno;
    }

    public void setAppno(int appno) {
        this.appno = appno;
    }

    /**
     * 获取
     *
     * @return
     */
    public List<ApplicationEnum> getServiceMenu() {
        String sql = "select t.menuid as id,t.menuname as caption from T_SYS_MENU t where t.belongtoapp = '13111414274386110001' and length(t.menuid) = 6";
        return DBUtil.queryAllBeanList(sql, ApplicationEnum.class);
    }

    public List<TSysMenu> getTSysMenus() {
//        PortalServiceServiceImpl pas = (PortalServiceServiceImpl) this.getBaseService();
        List<TSysMenu> tSysMenus = DBUtil.queryAllBeanList("select * from T_SYS_MENU t where t.belongtoapp = '13111414274386110001' and length(t.menuid) = 6", TSysMenu.class);
        return tSysMenus;
    }

    /**
     * 查询所有的服务类型
     *
     * @return
     */
    public String queryAllServiceType() {
        PortalServiceServiceImpl pas = (PortalServiceServiceImpl) this.getBaseService();
        List<TSysMenu> tSysMenus = DBUtil.queryAllBeanList("select * from T_SYS_MENU t where t.belongtoapp = '13111414274386110001' and length(t.menuid) = 6", TSysMenu.class);
        String appid = getRequest().getParameter("appid");
//        //用于绑定页面刷新后显示
//        String appno = getRequest().getParameter("appno");
//        List<TSysRole> rolelist = pas.queryAllServiceType(appid, appno);

        //默认第一次显示的接入系统为集合第一个
        List<TAccessAppReg> appRegs = pas.queryAllMenu();
        if (StringUtil.isBlank(appid)) {
            appid = appRegs.get(0).getAppid();
        }
        List<TSysMenu> menulist = pas.queryServiceMenu(appid);

        this.setParameter("appid", appid);
        this.setParameter("menulist", menulist);
        this.setParameter("tSysMenus", tSysMenus);
        return toView("servicemenu.jsp");
    }


    /**
     * 点击角色保存资源
     *
     * @return
     */
    public String saveRoleMenu() {
        PortalServiceServiceImpl pas = (PortalServiceServiceImpl) this.getBaseService();
        //得到存在的角色
        String serviceType = getRequest().getParameter("serviceType");
        String menues = getRequest().getParameter("menues");
        String[] menus = menues.split(",");
        String msg = "保存失败";
        try {
            msg = pas.addPortal(menus, serviceType);
            pas.load(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setParameter("msg", msg);
        return toView("list.jsp");
    }

    /**
     * @param request
     * @return
     */
    public String getMenues(HttpServletRequest request) {
        try {
            String appid = request.getParameter("appid");
            List<TLbxx> list = DBUtil.queryAllBeanList("select * from T_Lbxx where 1=1", TLbxx.class);
            String jsonStr = JsonUtil.getJSONString(list);
            return jsonStr;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
